<?php

namespace app\modules\Admin\models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class activityModel extends Model
{
    use SoftDeletes;

    protected $table = 'activity';
    protected $primaryKey = 'id';
    protected $appends = ['type_text', 'starttime_text', 'endtime_text', 'goods_ids_array'];

    protected $hidden = [];


    protected static array $types = [
        'seckill'=>'限时秒杀',
        'free_shipping'=>'满额包邮',
    ];

    /**
     * @param DateTimeInterface $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    /**
     * @return mixed|string
     */
    public function getTypeTextAttribute()
    {
        $type_text = '';
        $type_key = array_keys(self::$types);
        if(in_array($this->attributes['type'], $type_key)){
            $type_text = self::$types[$this->attributes['type']];
        }
        unset($type_key);
        return $type_text;
    }

    public function getStarttimeTextAttribute()
    {
        $starttime_text = '';
        if($this->attributes['starttime']){
            $starttime_text = date('Y-m-d H:i:s', $this->attributes['starttime']);
        }
        return $starttime_text;
    }

    public function getEndtimeTextAttribute()
    {
        $endtime_text = '';
        if($this->attributes['endtime']){
            $endtime_text = date('Y-m-d H:i:s', $this->attributes['endtime']);
        }
        return $endtime_text;
    }

    public function getgoodsIdsArrayAttribute()
    {
        return array_filter(explode(',', $this->attributes['goods_ids']));
    }

    public function richtext()
    {
        return $this->hasOne(\pagesModel::class, 'id', 'richtext_id');
    }

    public function activity_goods_sku_price()
    {
        return $this->hasMany(activitygoodsskupriceModel::class, 'activity_id', 'id');
    }
}